﻿namespace TradePublish.Common
{
    using System;
    using System.IO;
    using System.Net;
    using System.Text;

    public class GraspHTML
    {
        private string exception = "";

        public GraspHTML()
        {
            this.exception = "";
        }

        public string getException()
        {
            return this.exception;
        }

        public string getHTMLCode(string URL, CookieContainer cc)
        {
            this.exception = "";
            string str = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest) WebRequest.Create(URL);
                request.CookieContainer = cc;
                response = request.GetResponse() as HttpWebResponse;
                str = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")).ReadToEnd();
                response.Close();
            }
            catch (Exception exception)
            {
                this.exception = this.exception + "\n\t采集时发生的异常信息：" + exception.Message;
            }
            return str;
        }

        public CookieContainer getLoginCookie(string loginURL)
        {
            this.exception = "";
            HttpWebRequest request = null;
            CookieContainer container = null;
            try
            {
                request = (HttpWebRequest) WebRequest.Create(loginURL);
                request.Timeout = 0xc350;
                container = new CookieContainer();
                request.CookieContainer = container;
                (request.GetResponse() as HttpWebResponse).Close();
            }
            catch (Exception exception)
            {
                this.exception = this.exception + "\n\t登录时发生的异常信息：" + exception.Message;
            }
            return container;
        }
    }
}

